﻿/**********************************************************\
*  TinyLib - Entity                                        *
*                                                          *
*  Base class for graphical entities.                      *
*                                                          *
* (c) Jens Berglind                                        *
*                                                          *
* Last edited by Jens Berglind 25-06-2012                  *
\**********************************************************/ 

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using TinyLib.Sprite;

namespace TinyLib.Entity
{
    public class Entity : DrawableGameComponent, IDrawable, IUpdateable
    {
        #region Fields

        protected Texture2D texture = null;
        protected SpriteBatch spritebatch;
        public Color color = Color.White;
        private AnimatedSprite animatedSprite = null;
        public Rectangle rectangle;

        private bool isVisible = true;
        private bool killMe = false;

        public Vector2 direction = Vector2.Zero;
        public Vector2 position = Vector2.Zero;
        public Vector2 origin = Vector2.Zero;

        private float scale = 1.0f;
        public float rotation = 0.0f;
        public float speed = 1.0f;
        
        public float density = 0.0f;
        public float fGravity = 0.1f;
        public float fGravityAcceleration = 1.0f;

        public Rectangle screenBoundaries = new Rectangle();

        #endregion

        #region Properties

        public bool KillMe
        {
            get { return killMe; }
            set { killMe = value; }
        }

        public Rectangle Rectangle
        {
            get { return rectangle; }
            set { rectangle = value; }
        }

        public Vector2 Direction
        {
            get { return direction; }
            set { direction = value; }
        }

        public AnimatedSprite AnimatedSprite
        {
            get { return animatedSprite; }
            set { animatedSprite = value; }
        }

        public Color Color
        {
            get { return color; }
            set { color = value; }
        }

        public Vector2 Origin
        {
            get { return origin; }
            set { origin = value; }
        }

        public float Scale
        {
            get { return scale; }
            set { scale = value; }
        }

        public SpriteBatch Spritebatch
        {
            get { return spritebatch; }
        }

        public Texture2D texture2D
        {
            get { return texture; }
            set { texture = value; }
        }

        public float Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }

        public bool IsVisible
        {
            get { return isVisible; }
            set { isVisible = value; }
        }

        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        #endregion

        #region Init

        public Entity(Game game, SpriteBatch spritebatch)
            : base(game)
        {
            this.spritebatch = spritebatch;
        }

        #endregion

        #region Methods

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            
            if (AnimatedSprite != null)
            {
                position = AnimatedSprite.position;
                Vector2 size = new Vector2(texture2D.Width / AnimatedSprite.FrameSize.X * AnimatedSprite.scale.X, texture2D.Height / AnimatedSprite.FrameSize.Y * AnimatedSprite.scale.Y);
                Rectangle = new Rectangle((int)position.X, (int)position.Y, (int)size.X, (int)size.Y);
            }
            else if (texture2D != null)
            {
                Rectangle = new Rectangle((int)position.X, (int)position.Y, (int)texture2D.Width, (int)texture2D.Height);
            }
            else
            {
                //nothing to do
            }
        }

        public override void Draw(GameTime gameTime)
        {
            if (AnimatedSprite != null)
            {
                animatedSprite.Draw(gameTime);
            }
            else if (texture2D != null)
            {
                spritebatch.Draw(texture, position, null, color, rotation, origin, scale, SpriteEffects.None, 0.0f);
            }
            else
            {
                //nothing to draw
            }
            
        }
        /*
        public void DrawAnim(GameTime gameTime)
        {
            animatedSprite.Draw(gameTime);
        }
        */
       // public abstract void OnCollision();

        #endregion
    }
}
